﻿using System;
using System.Collections.Generic;
using System.Text;

namespace AMS.DataAccess.Sql
{
    /// <summary>
    /// 通过分隔符连接多个SQL语句，便于一次性执行
    /// </summary>
    public class SqlBatch
    {
        /// <summary>
        /// 
        /// </summary>
        public SqlBatch()
        {
            statements = new List<SqlStatement>();
        }

        /// <summary>
        /// 保存批量语句的容器
        /// </summary>
        private List<SqlStatement> statements;

        /// <summary>
        /// 默认的SQL语句之间的分隔字符串。其值为\r\n
        /// </summary>
        public readonly string DefaultSeperator = "\r\n";

        private string seperator;

        /// <summary>
        /// SQL语句之间的分隔字符串
        /// </summary>
        public string SqlSeperator
        {
            get { return string.IsNullOrEmpty(seperator) ? DefaultSeperator : seperator; }
            set { seperator = value; }
        }

        /// <summary>
        /// 获取一个值，表示当前批量中是否有语句要执行
        /// </summary>
        public bool IsEmpty
        {
            get { return statements.Count == 0; }
        }

        /// <summary>
        /// 获取或设置一个值，表示是否紧凑模式。将影响由它创建的 sql 对象
        /// </summary>
        public bool CompactMode { get; set; }

        /// <summary>
        /// 创建并添加SqlInsert对象。
        /// </summary>
        /// <param name="tableName">提供给SqlInsert的表名参数</param>
        /// <returns></returns>
        public SqlInsert CreateSqlInsert(string tableName)
        {
            SqlInsert insert = new SqlInsert(tableName);
            insert.CompactMode = CompactMode;
            statements.Add(insert);
            return insert;
        }

        /// <summary>
        /// 创建并添加SqlDelete对象。
        /// </summary>
        /// <param name="tableName">提供给SqlDelete的表名参数</param>
        /// <returns></returns>
        public SqlDelete CreateSqlDelete(string tableName)
        {
            SqlDelete delete = new SqlDelete(tableName);
            delete.CompactMode = CompactMode;
            statements.Add(delete);
            return delete;
        }

        /// <summary>
        /// 创建并添加SqlUpdate对象。
        /// </summary>
        /// <param name="tableName">提供给SqlUpdate的表名参数</param>
        /// <returns></returns>
        public SqlUpdate CreateSqlUpdate(string tableName)
        {
            SqlUpdate update = new SqlUpdate(tableName);
            update.CompactMode = CompactMode;
            statements.Add(update);
            return update;
        }

        /// <summary>
        /// 创建并添加SqlBuilder对象。
        /// </summary>
        /// <returns></returns>
        public SqlBuilder CreateSqlBuilder()
        {
            return CreateSqlBuilder(null);
        }

        /// <summary>
        /// 创建并添加SqlBuilder对象。
        /// </summary>
        /// <param name="tableName">提供给SqlBuilder的表名参数</param>
        /// <returns></returns>
        public SqlBuilder CreateSqlBuilder(string tableName)
        {
            SqlBuilder sb = new SqlBuilder(tableName);
            sb.CompactMode = CompactMode;
            statements.Add(sb);
            return sb;
        }

        /// <summary>
        /// 添加SQL语句
        /// </summary>
        /// <param name="statement"></param>
        public void Add(SqlStatement statement)
        {
            statements.Add(statement);
        }

        /// <summary>
        /// 清除批量中的所有语句
        /// </summary>
        public void Clear()
        {
            statements.Clear();
        }

        /// <summary>
        /// 返回用分号拼接的SQL语句
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            StringBuilder sbSql = new StringBuilder();
            foreach (SqlStatement statement in statements)
            {
                string sql = statement.ToString();
                if (sql.Length > 0)
                {
                    if (sbSql.Length == 0)
                    {
                        sbSql.Append(sql);
                    }
                    else
                    {
                        sbSql.AppendLine(SqlSeperator);
                        sbSql.Append(sql);
                    }
                }
            }

            return sbSql.ToString();
        }
    }
}
